class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int n=nums.size();
        int count=0;
        for(auto& ch:nums)
        {
            count+=ch;
        }
        vector<int> dp1(n);
        vector<int> dp2(n);
        dp1[0]=nums[0];
        int ret1=nums[0];
        int ret2=nums[0];
        for(int i=1;i<n;++i)
        {
            dp1[i]=max(nums[i],dp1[i-1]+nums[i]);
            ret1=max(ret1,dp1[i]);
        }
        dp2[0]=nums[0];
        for(int i=1;i<n;++i)
        {
            dp2[i]=min(nums[i],dp2[i-1]+nums[i]);
            ret2=min(ret2,dp2[i]);
        }
        if(ret1<0&&ret2<0) return ret1;
        return max(ret1,count-ret2);
    }
};
